/**
 * 
 */
$(document).ready(function() {

	getRefundableAmount();

	$('#queryOrder').on('click', function() {
		getRefundableAmount();
	});

	$('#refundBtn').on('click', function() {
		return submitRefundForm();
	});
});

/**
 * 提交退款请求
 */
function submitRefundForm() {

	if ($('#refundInfoForm').length !== 0) {
		var amount = $('#amount').val();
		var avaliableAmount = $('#avaliableAmount').val();
		if (amount === '') {
			alert('请输入退款金额');
		} else if (amount > avaliableAmount) {
			alert('退款金额不能大于可退款金额');
		} else {
			$.ajax({
				url : '/shop-admin/refund/refund',
				type : 'POST',
				data : $('#refundInfoForm').serialize(),
				dataType : "json"
			}).done(function(data) {
				if (data.succ == false) {
					if (data.msg == null) {
						alert("订单无支付记录，请联系开发人员！")
					} else {
						alert(data.msg);
					}
				} else if (data.succ == true) {
					location.href = document.referrer;
				} else {
					alert('系统异常，请通知开发人员查看日志！');
				}
			});
		}
		// 禁止表单提交
		return false;
	}
}

/**
 * 获取退款金额
 */
function getRefundableAmount() {

	if ($('#orderNumber').length !== 0) {
		var orderNumber = $('#orderNumber').val();
		if (orderNumber !== '') {
			$.ajax(
					{
						url : '/shop-admin/refund/orderinfo?order.orderNumber='
								+ orderNumber,
						dataType : "json"
					}).done(function(data) {
				if (data.ret === 'success') {
					$('#totalAmount').val(data.orderAmount);
					$('#avaliableAmount').val(data.availableAmount);
				} else if (data.ret === 'fail') {
					alert(data.error);
				}
			});
		}
	}
}